package com.xdf.whiteaccount.service.impl;

import com.xdf.whiteaccount.enums.BillTypeDyeGrey;
import com.xdf.whiteaccount.service.BillCodeService;
import com.xdf.whiteaccount.service.DyeGreyMainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.context.annotation.Lazy;
import com.xdf.whiteaccount.dao.DyeGreyMainMapper;
import com.xdf.whiteaccount.entity.DyeGreyMain;
import com.xdf.whiteaccount.utils.BusinessException;
import com.xdf.whiteaccount.utils.Example;
import com.xdf.whiteaccount.utils.BeanUtils;
import com.xdf.whiteaccount.enums.OpeEnum;
import com.xdf.whiteaccount.enums.MoveBillFlag;

/**
 * @Description : 服务层实现类，由代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2023-12-15 16:05:38
 */
@Slf4j
@Service
public class DyeGreyMainServiceImpl implements DyeGreyMainService {
    @Autowired
    private DyeGreyMainMapper dao;

    @Lazy
    @Autowired
    private DyeGreyMainServiceImpl meService;
    @Autowired
    private BillCodeService billCodeService;

    @Override
    @Transactional
    public DyeGreyMain insert(DyeGreyMain record) {
        record = meService.fullFill(record);
        meService.doBeforeInsert(record);
        dao.insert(record);
        meService.doAfterInsert(record);
        return record;
    }

    @Override
    @Transactional
    public DyeGreyMain insertSelective(DyeGreyMain record) {
        record = meService.fullFill(record);
        meService.doBeforeInsert(record);
        dao.insertSelective(record);
        meService.doAfterInsert(record);
        return record;
    }

    /* 由于本方法无法返回所有对象的自增主键，后者调整为一个一个保存的方法
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<DyeGreyMain> list) {
        return dao.multiInsert(list);
    }
     */
    @Override
    @Transactional
    public List<DyeGreyMain> multiInsert(List<DyeGreyMain> list) {
        list.forEach(item -> meService.insertSelective(item));
        return list;
    }

    @Override
    public int updateByPrimaryKey(DyeGreyMain record) {
        try {
            BeanUtils.fullFillRecord(record, OpeEnum.UPDATE);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(DyeGreyMain record) {
        try {
            BeanUtils.fullFillRecord(record, OpeEnum.UPDATE);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        DyeGreyMain dbRecord = dao.selectByPrimaryKey(id);
        if( dbRecord == null ) return 0;
        dbRecord.setState(-1);
        return dao.updateByPrimaryKey(dbRecord);
    }

    @Override
    public DyeGreyMain selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    // 根据主键作废
    @Override
    public int setVoid(Integer id){
        DyeGreyMain dbRecord = dao.selectByPrimaryKey(id);
        if( dbRecord == null ) return 0;
        dbRecord.setState(0);
        return dao.updateByPrimaryKey(dbRecord);
    }

    @Override
    public List<DyeGreyMain> selectAll(){
        return dao.selectAll();
    }

    @Override
    public DyeGreyMain save(DyeGreyMain record){
        if(record == null) throw new BusinessException("空对象不可保存！");
        Integer iid = Optional.ofNullable(record.getId()).orElse(0);
        if(iid==0) record = insertSelective(record);
        else updateByPrimaryKeySelective(record);
        return record;
    }

    // 没有使用 xml 中的 multiSave 方法是因为无法批量回写自增主键
    @Transactional
    @Override
    public List<DyeGreyMain> multiSave(List<DyeGreyMain> list){
        if(list == null || list.size()==0) return list;
        list.forEach(item -> item = meService.save(item));
        return list;
    }

    /**=========================下面开始是固定模式代码=======================================*/
    // 新增前检测，不可新增则抛出异常
    private void check4Insert(DyeGreyMain record){
    }

    @Override
    public DyeGreyMain fullFill(DyeGreyMain record) {
        try {
            // 根据“新增”或者“修改”填充创建人或者最后一次修改人
            // 要求表格中有字段：create_user,last_user
            BeanUtils.fullFillRecord(record,OpeEnum.NEW);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        // 根据单据类型填充单据编号
        String billType = Optional.ofNullable(record.getBillType()).orElse("");
        if(billType.length() == 0) return record;

        String newBillCode = billCodeService.getNewBillCode(billType);
        record.setBillCode(newBillCode);
        return record;
    }

    @Override
    public void doBeforeInsert(DyeGreyMain record) {
        // TODO: 在新增数据前需要做的逻辑写在这里，抛出异常则终止新增操作
        DyeGreyMainService.super.doBeforeInsert(record);
    }

    @Override
    public void doAfterInsert(DyeGreyMain record) {
        // TODO: 新增数据后需要做的逻辑写在这里，抛出异常则终止新增操作
        DyeGreyMainService.super.doAfterInsert(record);
    }

    @Override
    public List<DyeGreyMain> selectByExample(Example<DyeGreyMain> example){
        return dao.selectByExample(example);
    }

    @Override
    public DyeGreyMain moveBill(Integer id, String moveBillFlag){
        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.FIRST.getFlag())){
            DyeGreyMain first = dao.getFirst();
            if(first == null) throw new BusinessException("没有单据！");
            else return first;
        }

        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.LAST.getFlag())){
            DyeGreyMain last = dao.getLast();
            if(last == null) throw new BusinessException("没有单据！");
            else return last;
        }
        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.NEXT.getFlag())){
            DyeGreyMain next = dao.getNext(id);
            if(next == null) throw new BusinessException("没有下一单了！");
            else return next;
        }

        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.PREVIOUS.getFlag())){
            DyeGreyMain prev = dao.getPrev(id);
            if(prev == null) throw new BusinessException("没有上一单了！");
            else return prev;
        }

        return null;
        }
    //=================以上是自动生成的代码，自定义代码请写在下面==================

    @Override
    public DyeGreyMain getNew8ClientSid(String clientSid, BillTypeDyeGrey billType){
        if(billType == null) billType = BillTypeDyeGrey.TRANS_SELF;
        String newBillCode = billCodeService.getNewBillCode(billType.getFlag());

        DyeGreyMain dyeGreyMain = new DyeGreyMain();
        dyeGreyMain.setClient(clientSid);
        dyeGreyMain.setBillCode(newBillCode);
        dyeGreyMain.setBillDate(new Date());
        dyeGreyMain.setBillType(billType.getFlag());
        return dyeGreyMain;
    }
}